package sng.modules.view.components.city.logic.unit
{
	import as3isolib.display.IsoSprite;
	
	import core.interfaces.IDispose;
	
	import sng.modules.view.components.city.logic.City;
	import sng.modules.view.components.city.logic.CityView;
	import sng.modules.view.components.city.logic.event.TileEvent;
	import sng.modules.view.components.city.logic.interfaces.ISheet;
	import sng.modules.view.components.city.logic.movement.BasicMove;
	import sng.modules.view.components.city.logic.movement.TileMovement;
	
	
	public class Tile extends IsoSprite implements IDispose
	{
		private var _cityView:CityView;
		private var _selected:Boolean;
		private var _column:Number;
		private var _row:Number;
		private var _name:String = "Tile";
		
		public function Tile(descriptor:Object=null)
		{
			super(descriptor);
		}
		
		override public function setSize(width:Number, length:Number, height:Number):void
		{
			super.setSize(City.GRID_CELLSIZE * width, City.GRID_CELLSIZE * length, City.GRID_CELLSIZE * height);
			
			invalidateSprites();
		}
	 	public function  spawnAt(col:Number, row:Number, _z:Number = 0):void
		{
			this.x = col * City.GRID_CELLSIZE;
			this.y = row * City.GRID_CELLSIZE;
			this.z = _z;
		}

		public function get cityView():CityView
		{
			return _cityView;
		}

		public function set cityView(value:CityView):void
		{
			_cityView = value;
		}

		public function get selected():Boolean
		{
			return _selected;
		}

		public function set selected(value:Boolean):void
		{
			_selected = value;
			trace(name + " selected " + _selected);
			if (_selected)
			{
				dispatchEvent(new TileEvent(TileEvent.SELECTED));
			}
			else
			{
				dispatchEvent(new TileEvent(TileEvent.DESELECTED));
			}
			
			invalidateSprites();
		}

		override public function get name():String
		{
			return _name;
		}

		override public function set name(value:String):void
		{
			_name = value;
		}

		public function get column():Number
		{
			return _column;
		}

		public function set column(value:Number):void
		{
			_column = value;
		}

		public function get row():Number
		{
			return _row;
		}

		public function set row(value:Number):void
		{
			_row = value;
		}
		
		public function get uniqueName():String
		{
			return _name + "_" + positionAsString;
		}
		
		/**********************************************
		 * GRID-RELATED
		 **********************************************/
		
		public function get positionAsString():String
		{
			return String(column) + "_" + String(row) + "_" + String(z);
		}
		
		public function dispose():void
		{
			
		}
		
		//初始化格子位置
		override public function invalidatePosition():void
		{
			super.invalidatePosition();
			
			if (Math.floor(x / City.GRID_CELLSIZE) == this.column && Math.floor(y / City.GRID_CELLSIZE) == this.row)
			{
				return;
			}
			
			this.column = Math.floor(x / City.GRID_CELLSIZE);
			this.row = Math.floor(y / City.GRID_CELLSIZE);
			
		}
	}
}